/**
 * Created by zhaoyanzhi on 16-10-9.
 */
app.controller("paymentPage",function($scope,$location,$filter,$q){
    $scope.date=new Date();
    $filter("date")($scope.date, "yyyy年MM月dd日 HH:mm:ss");
    $scope.getUserId = function () {
        var userId = JSON.parse(localStorage.getItem("AV/RaXOd37qSYs8fKCN82nbC9zd-gzGzoHsz/currentUser")).objectId;
        return userId
    };
    $scope.addButtonEvent=function(){
        var userId = $scope.getUserId();
        var user = AV.Object.createWithoutData('User', userId);
        var relation = user.relation('productInfo');
        var query = relation.query();
        query.find().then(function(results){
            for(var i=0;i<results.length;i++){
                results[i].destroy();
            }
            $location.path("/productListPage")
        });

    };
    $scope.showSumSavePrice=function(data){
        var sumSavePrice=ShoppingCart.computeSumSavePrice(data);
        $scope.savePrice=sumSavePrice;
        return  sumSavePrice
    };
    $scope.showActuallyPaid=function(data){
        var  sumActuallyPaid=ShoppingCart.computeSumActuallyPaidPrice(data);
            $scope.sumPrice=sumActuallyPaid;
    };
    $scope.showShoppingList=function(){
        var defer = $q.defer();
        var promise = defer.promise;
        var userId = $scope.getUserId();
        var user = AV.Object.createWithoutData('User', userId);
        var relation = user.relation('productInfo');
        var query = relation.query();
        promise.then(function(data){
            $scope.cartList=data ;
            $scope.showSumSavePrice(data)
            $scope.showActuallyPaid(data);
        });
        defer.resolve( query.find().then(function(results){
                var data=[];
                for(var i=0;i<results.length;i++){
                    data.push(results[i].attributes)
                }
                return data
            }
        ));
    };
    $scope.showShoppingList();
})